package task

import (
	"time"

	"file_explorer/common/log"
)

type PollTask struct {
	BasicTask

	fun      func()
	duration int
}

func (task *PollTask) start() {
	task.BasicTask.start()
	defer task.BasicTask.finish()
	for {
		if task.err != nil {
			break
		}
		log.Debug("poll task execute")
		task.fun()
		time.Sleep(time.Duration(task.duration) * time.Millisecond)
	}
}

// 轮询任务
func NewPollTask(duration int, fun func()) *PollTask {
	return &PollTask{
		BasicTask: newBasicTask("poll task"),
		fun:       fun,
		duration:  duration,
	}
}
